//
//  AlertElementsViewDJ.m
//  RTUApp
//
//  Created by 陈德军 on 2017/6/22.
//  Copyright © 2017年 陈德军. All rights reserved.
//

#import "AlertElementsViewDJ.h"

#define HEXColor(hexValue,alphaValue) [UIColor colorWithRed:((float)((hexValue & 0xFF0000) >> 16))/255.0 green:((float)((hexValue & 0xFF00) >> 8))/255.0 blue:((float)(hexValue & 0xFF))/255.0 alpha:alphaValue]
#define autoScaleW(width) [(AppDelegate *)[UIApplication sharedApplication].delegate autoScaleW:width]
#define autoScaleH(height) [(AppDelegate *)[UIApplication sharedApplication].delegate autoScaleH:height]



@interface AlertElementsViewDJ(){
    
}
@property (nonatomic,strong) UIWindow * backWindow;
@property (nonatomic,strong ) UIView *selctview;
/**
 *  是否选择数组
 */
@property (nonatomic,strong) NSMutableArray *switchArray;
@property (nonatomic,assign) BOOL sec;
@end

@implementation AlertElementsViewDJ

- (UIWindow*)backWindow{
    if (!_backWindow) {
        _backWindow=[[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _backWindow.windowLevel=UIWindowLevelAlert;
        [_backWindow becomeKeyWindow];
        [_backWindow makeKeyAndVisible];
    }
    return _backWindow;
}

- (instancetype)init{
    self = [super init];
    self.frame = [UIScreen mainScreen].bounds;
    
    [self setBackgroundColor:[UIColor blackColor]];
    self.backgroundColor = [UIColor clearColor];
    self.switchArray = [NSMutableArray new];
    NSArray *arr = @[@"128",@"64",@"32",@"16",@"8",@"4",@"2",@"1"];
    for (int i=0; i<8; i++) {
        [self.switchArray addObject:arr];
    }
    [self addView];
    return self;
}



- (void)addView{
    UIView *view = [UIView new];
    view.size = CGSizeMake(kScreenWidth, 500);
    view.top = 100;
    view.left = 0;
    view.backgroundColor = [UIColor colorWithRed:1.00 green:1.00 blue:0.88 alpha:1.00];
    [self addSubview:view];
    _backWindow=[[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _backWindow.windowLevel=UIWindowLevelAlert;
    [_backWindow becomeKeyWindow];
    [_backWindow makeKeyAndVisible];
    _selctview = view;
    
    UIScrollView *scrollV = [UIScrollView new];
    scrollV.backgroundColor = [UIColor clearColor];
    scrollV.size = CGSizeMake(kScreenWidth-100-15, 380);
    scrollV.left = 115;
    scrollV.top = 0;
    scrollV.contentSize = CGSizeMake(800, 380);
    [view addSubview:scrollV];
    
    
    UIButton *cancelBut = [UIButton new];
    cancelBut.backgroundColor = [UIColor grayColor];
    [cancelBut addTarget:self action:@selector(cleckcancelButEvent) forControlEvents:(UIControlEventTouchUpInside)];
    cancelBut.size = CGSizeMake(100, 44);
    cancelBut.left = (kScreenWidth-240)/2;
    cancelBut.top = 400;
    [cancelBut setTitle:@"取消" forState:(UIControlStateNormal)];
    [view addSubview:cancelBut];
    
    UIButton *commitBut = [UIButton new];
    commitBut.backgroundColor = [UIColor grayColor];
    [commitBut addTarget:self action:@selector(cleckcommitButEvent) forControlEvents:(UIControlEventTouchUpInside)];
    commitBut.size = CGSizeMake(100, 44);
    commitBut.left = cancelBut.right + 40;
    commitBut.top = cancelBut.top;
    [commitBut setTitle:@"提交" forState:(UIControlStateNormal)];
    [view addSubview:commitBut];
    
    NSArray *titleArr = @[@"要素号",@"第一要素",@"第二要素",@"第三要素",@"第四要素",@"第五要素",@"第六要素",@"第七要素",@"第八要素",];
    MyLinearLayout *titleLayout = [MyLinearLayout linearLayoutWithOrientation:MyLayoutViewOrientation_Vert];
    titleLayout.myTop = 10;
    titleLayout.myLeft = 10;
    titleLayout.myWidth = 100;
    titleLayout.wrapContentWidth = YES;
    [view addSubview:titleLayout];
    for (int i = 0; i<titleArr.count; i++) {
        UILabel *titleLable = [UILabel new];
        titleLable.textColor = [UIColor blackColor];
        titleLable.size = CGSizeMake(70, 30);
        titleLable.myLeft = 10;
        titleLable.myTop = 10;
        titleLable.text = titleArr[i];
        [titleLayout addSubview:titleLable];
        
    }
    
    
    
    MyLinearLayout *detailLayout = [MyLinearLayout linearLayoutWithOrientation:MyLayoutViewOrientation_Vert];
    detailLayout.myTop = 10;
    detailLayout.myLeft = 0;
    detailLayout.myWidth = 1100;
    detailLayout.wrapContentWidth = YES;
    [scrollV addSubview:detailLayout];
    
    NSArray *detailArr = @[@"D7",@"D6",@"D5",@"D4",@"D3",@"D2",@"D1",@"D0",@"降水量",@"蒸发了",@"风向",@"风速",@"气温",@"温度",@"地温",@"气压",@"水位8",@"水位7",@"水位6",@"水位5",@"水位4",@"水位3",@"水位2",@"水位1",@"地下水埋深",@"图片",@"波浪",@"匣门开度",@"水量",@"流速",@"流量",@"水压",@"水表8",@"水表7",@"水表6",@"水表5",@"水表4",@"水表3",@"水表2",@"水表1",@"100CM墒情",@"80CM墒情",@"60CM墒情",@"50CM墒情",@"40CM墒情",@"30CM墒情",@"20CM墒情",@"10CM墒情",@"PH值",@"溶解氧",@"电导率",@"浊度",@"氧化还原",@"高锰酸钾",@"氨氮",@"水温",@"总有机碳",@"总氧",@"总磷",@"锌",@"硒",@"砷",@"总泵",@"镉",@"",@"",@"",@"",@"",@"叶绿素a",@"铜",@"铅"];
    for (int i=0; i<9; i++) {
        MyLinearLayout *detailSubLayout = [MyLinearLayout linearLayoutWithOrientation:MyLayoutViewOrientation_Horz];
        detailSubLayout.myTop = 10;
        detailSubLayout.wrapContentHeight = YES;
        [detailLayout addSubview:detailSubLayout];
        
        for (int j=0; j<8; j++) {
            if (i==0) {
                UILabel *detaleLabel = [UILabel new];
                detaleLabel.myLeft = 0;
                detaleLabel.myTop = 0;
                detaleLabel.size = CGSizeMake(133, 30);
                detaleLabel.text = detailArr[j+i*8];
                [detailSubLayout addSubview:detaleLabel];
            }else{
                UISwitch *switcV = [UISwitch new];
                switcV.on = YES;
                [switcV addTarget:self action:@selector(switchEvent:) forControlEvents:(UIControlEventTouchUpInside)];
                switcV.tag = 1000+j+(i-1)*10;
                [detailSubLayout addSubview:switcV];
                
                UILabel *detaleLabel = [UILabel new];
                detaleLabel.myLeft = 0;
                detaleLabel.myTop = 0;
                detaleLabel.size = CGSizeMake(80, 30);
                detaleLabel.text = detailArr[j+i*8];
                [detailSubLayout addSubview:detaleLabel];
            }
            
            
            
        }
        
    }
    
}

- (void)switchEvent:(id)sender{
    UISwitch *switchButton = (UISwitch*)sender;
    BOOL isButtonOn = [switchButton isOn];
    int j = ((int)switchButton.tag -1000)/10;
    int k = ((int)switchButton.tag -1000)%10;
    if (isButtonOn) {
        NSMutableArray *arr = [NSMutableArray arrayWithArray:self.switchArray[j]];
        int num=1;
        for (int i=0; i<-(k-7); i++) {
            num=2*num;
        }
        [arr replaceObjectAtIndex:k withObject:[NSString stringWithFormat:@"%d",num]];
        [self.switchArray replaceObjectAtIndex:j withObject:arr];
    }else {
        NSMutableArray *arr = [NSMutableArray arrayWithArray:self.switchArray[j]];
        [arr replaceObjectAtIndex:k withObject:@"0"];
        [self.switchArray replaceObjectAtIndex:j withObject:arr];
    }
}

- (void)show{
    [self.backWindow addSubview:self];
    [self layerAnimationMakeWithUp:YES];
    CGPoint startPoint = CGPointMake(self.center.x, -_selctview.frame.size.height);
    _selctview.layer.position=startPoint;
    
    //damping:阻尼，范围0-1，阻尼越接近于0，弹性效果越明显
    //velocity:弹性复位的速度
    [UIView animateWithDuration:.8 delay:0 usingSpringWithDamping:.6 initialSpringVelocity:1.0 options:UIViewAnimationOptionCurveLinear animations:^{
        _selctview.layer.position=self.center;
        
    } completion:^(BOOL finished) {
        
    }];
    
}
- (void)dismissAlertView{
    [self layerAnimationMakeWithUp:NO];
    [UIView animateWithDuration:0.15 animations:^{
        
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        [_backWindow resignKeyWindow];
        _backWindow.hidden = YES;
        
    }];
    
}


-(void)layerAnimationMakeWithUp:(BOOL)up{
    [self.layer removeAllAnimations];
    CABasicAnimation *colorAnimation = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
    
    if (up == YES) {
        colorAnimation.duration = 0.3;
        colorAnimation.fromValue = (__bridge id _Nullable)([HEXColor(0x000000, 0) CGColor]);
        colorAnimation.toValue = (__bridge id _Nullable)([HEXColor(0x000000, 0.4) CGColor]);
    }else{
        colorAnimation.duration = 0.15;
        colorAnimation.fromValue = (__bridge id _Nullable)([HEXColor(0x000000, 0.4) CGColor]);
        colorAnimation.toValue = (__bridge id _Nullable)([HEXColor(0x000000, 0) CGColor]);
    }
    
    
    colorAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    colorAnimation.fillMode = kCAFillModeForwards;
    colorAnimation.removedOnCompletion = NO;
    [self.layer addAnimation:colorAnimation forKey:@"colorAnimation"];
    
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    if (_sec) {
        return;
    }
    [self dismissAlertView];
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}

#pragma mark 触发事件
- (void)cleckcommitButEvent{
    [self dismissAlertView];
    NSMutableString *str = [NSMutableString new];
    for (int i=0; i<self.switchArray.count; i++) {
        NSMutableArray *arr = [NSMutableArray arrayWithArray:self.switchArray[i]];
        int num=0;
        for (int j=0; j<8; j++) {
            num = num+[arr[j] intValue];
        }
        if (i==0) {
            str = [NSMutableString stringWithFormat:@"%@",[self ToHex:num]];
        }else{
            str = [NSMutableString stringWithFormat:@"%@,%@",str,[self ToHex:num]];
        }
        
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(delegateCommitButEvent:)]) {
        [self.delegate delegateCommitButEvent:str];
    }
}

- (void)cleckcancelButEvent{
    [self dismissAlertView];
}


-(NSString *)ToHex:(long long int)tmpid
{
    NSString *nLetterValue;
    NSString *str =@"";
    long long int ttmpig;
    for (int i = 0; i<9; i++) {
        ttmpig=tmpid%16;
        tmpid=tmpid/16;
        switch (ttmpig)
        {
            case 10:
                nLetterValue =@"A";break;
            case 11:
                nLetterValue =@"B";break;
            case 12:
                nLetterValue =@"C";break;
            case 13:
                nLetterValue =@"D";break;
            case 14:
                nLetterValue =@"E";break;
            case 15:
                nLetterValue =@"F";break;
            default:nLetterValue=[[NSString alloc]initWithFormat:@"%lli",ttmpig];
                
        }
        str = [nLetterValue stringByAppendingString:str];
        if (tmpid == 0) {
            break;
        }
        
    }
    return str;
}
@end
